home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 3144 < prev    next >
Encoding:
Text File  |  1996-08-06  |  3.0 KB  |  132 lines

  1. Path: svnews.ubinet.ubs.com!ubszh!ubszh!jis
  2. From: jis@ubszh.net.ch (Johnston Ian (by ubsswop))
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: [] overload..(newbie in distress)
  5. Date: 22 Jan 1996 13:59:32 GMT
  6. Organization: UBS
  7. Distribution: world
  8. Message-ID: <4e0584$1k3@ubszh.fh.zh.ubs.com>
  9. References: <DLGppJ.31C@undergrad.math.uwaterloo.ca> <Robert.Lendvai-2101960118330001@129.170.80.94>
  10. NNTP-Posting-Host: nol2179.fh.zh.ubs.com
  11.  
  12. In article <Robert.Lendvai-2101960118330001@129.170.80.94>, Robert.Lendvai@dartmouth.edu (Robert Lendvai) writes:
  13. |> In article <DLGppJ.31C@undergrad.math.uwaterloo.ca>,
  14. |> tthiraku@landen.math.uwaterloo.ca (Thanou Thirakul) wrote:
  15. |> 
  16. |> > Hi..
  17. |> > 
  18. |> > I'm currently stuck on how to overload [] operator such that
  19. |> > it does two different tasks..
  20. |> > 
  21. |> > 
  22. |> > case 1:
  23. |> > 
  24. |> > // A is an instance of a class that contains a linkist.
  25. |> > 
  26. |> >    A[5] = val;  // store val into the fifth node of a linklist. 
  27. |> >    val = A[5] ; // returns the value of the fifth node of a linklist.  
  28. |> > 
  29. |> > 
  30. |> > I was wondering how can C++ differentiate these two senerios? 
  31. |> > 
  32. |> > Anything help will greatly appreciated..
  33. |> > 
  34. |> > Thank You Kindly
  35. |> > -- 
  36. |> > Thanou Thirakul
  37. |> > tthiraku@undergrad.math.uwaterloo.ca
  38. |> > http://www.undergrad.math.uwaterloo.ca/~tthiraku
  39. |> >  ___________________________________________________________
  40. |> > |                                                           |
  41. |> > | <after submitting a CS assignment>                        |
  42. |> > |                                                           |
  43. |> > |  "Well, that was the most well-documented non-working     |
  44. |> > |   piece of code I have ever written.. " -unknown          |
  45. |> > |                                                           |
  46. |> > |___________________________________________________________|
  47. |> 
  48. |> The = assignment operator can not be overloaded.
  49.  
  50. The = assignment operator can be overloaded, actually, but that is not
  51. the answer to this question.
  52.  
  53. You need to make operator[] return a new object; that new object handles
  54. operator =() and operator T().
  55.  
  56. For example, assuming you are storing floats:
  57.  
  58. class ARef;
  59.  
  60. class A
  61. {
  62.   public:
  63.     // Whatever
  64.     ARef operator[](int index);
  65.  
  66.     void putAt(int index, float f)
  67.     {
  68.     // Store f at index.
  69.     }
  70.  
  71.     float getAt(int index)
  72.     {
  73.     // REtrieve float at index.
  74.     }
  75. };
  76.  
  77.  
  78. class ARef
  79. {
  80.   public:
  81.     ARef(A &a, int i)
  82.     : aObj(a),
  83.       index(i)
  84.     {
  85.     }
  86.  
  87.     void operator = (float f)
  88.     {
  89.     aObj.putAt(index, f);
  90.     }
  91.  
  92.     operator float()
  93.     {
  94.     aObj.getAt(index);
  95.     }
  96.  
  97.   private:
  98.     A &aObj;
  99.     int index;
  100. };
  101.  
  102.  
  103. ARef A::operator [](int index)
  104. {
  105.     return ARef(*this, index);
  106. }
  107.  
  108.  
  109. Now
  110.  
  111.     a[5] = val;
  112.  
  113. becomes equivalent to:
  114.  
  115.     ARef tmp = a[5];
  116.     tmp = val;        // ARef::operator = (float);
  117.  
  118. and
  119.  
  120.     val = a[5];
  121.  
  122. becomes equivalent to:
  123.  
  124.     ARef tmp = a[5];
  125.     val = tmp;        // ARef::operator float();
  126.  
  127.  
  128. Beware of performance implications if you use this in tight loops and
  129. your compiler does not inline well.
  130.  
  131. Ian
  132.